var express=require('express');
var app =express();
var fs = require('fs'); // 引入fs模块

let bodyParser=require('body-parser');
//此中中间件的作用是获得请求体字符串，然后转成对象赋值给req.body
app.use(bodyParser.urlencoded({extended:true}));
//判断请求体格式是不是json格式，如果是的话会调用JSON.parse方法把请求体字符串转成对象
app.use(bodyParser.json());


//设置跨域访问
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});


//写个接口123
app.get('/123/:id',function(req,res){
    console.log('id', req.params.id) // 获取参数
    res.status(200),
        res.json('')
});

//写个接口345
app.post('/345',function(req,res){
    console.log('id', req.body) // 获取参数
    res.status(200),
        res.json({"text": "成功"})
});


//配置服务端口
var server = app.listen(9000, function () {

    var host = server.address().address;

    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);
})